package com.classtwo.colleage.controler;

import ch.qos.logback.core.util.FileUtil;
import com.classtwo.colleage.model.Student;
import com.classtwo.colleage.tool.ExcelHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@Controller
public class HandlerExcel {
    @RequestMapping("/export")
    public void export(HttpServletResponse response){

        //模拟从数据库获取需要导出的数据
        List<Student> studentList = new ArrayList<>();
        Student student1=new Student("王晓峰", "1");
        Student student2=new Student("黄仲浩", "2");
        Student student3=new Student("刘东", "3");
        Student student4=new Student("叶俊波", "4");

        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        studentList.add(student4);

        ExcelHandler.exportExcel(studentList,"花名册","草帽一伙",Student.class,"海贼王1.xls",response);
        //导出操作
/*        FileUtil.exportExcel(personList,"花名册","草帽一伙",Person.class,"海贼王.xls",response);*/
    }

    @RequestMapping("importExcel")
    public void importExcel(){
        String filePath = "E:\\海贼王1.xls";
        //解析excel，

        List<Student> personList = ExcelHandler.importExcel(filePath,1,1,Student.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+personList.size()+"】行");

        //TODO 保存数据库
    }



}
